<?php
ob_start();
session_start();
include("includes/dbconnection.php");

if(isset($_POST['conbtn']) && $_REQUEST['_mod']==1)
{
	$maincont = addslashes($_POST['maincont']);
	mysql_query("update homecontent set Content='$maincont'") or die(mysql_error());
	//header("location:index.php?_s1=true");
	echo "<script>window.location='index.php?_s1=true'</script>";
}

if(isset($_POST['rimgbtn']) && $_REQUEST['_mod']==2)
{
	$r1 = $_FILES["r1"]["name"];
	$r2 = $_FILES["r2"]["name"];
	$path = "homepageimg/";
	$error = "";
	if($r1 != "")
	{
		$q = mysql_fetch_array(mysql_query("select * from homecontent limit 1"));
		if($_FILES['r1']['type'] == "image/gif" || $_FILES['r1']['type'] == "image/pjpeg" || $_FILES['r1']['type'] == "image/jpeg")
		{
			$newupload = $_FILES['r1']['tmp_name'];
			list($width,$height)=getimagesize($newupload);
			
				$delpath1 = "homepageimg/".$q["RightImage1"];
				if(file_exists($delpath1)) unlink($delpath1);
				if($_FILES['r1']['type'] == "image/gif") $filetype = ".gif";
				else  $filetype = ".jpg";
				
				if($filetype==".gif") $src = imagecreatefromgif($newupload);
				else $src = imagecreatefromjpeg($newupload);
				list($width,$height)=getimagesize($newupload);
				
				if($width != 57 && $height !=85)
				{
					$newwidth = 57;
					$newheight = 85;
				}
				else
				{
					$newwidth = $width;
					$newheight = $height;
				}
				
				$tmp=imagecreatetruecolor($newwidth,$newheight);
				imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
				$photo1  = "r1".$filetype;
				$filename = $path.$photo1; 
				if($filetype==".gif") imagegif($tmp,$filename,100);
				else imagejpeg($tmp,$filename,100);
				imagedestroy($src);
				imagedestroy($tmp);
				
				mysql_query("update homecontent set RightImage1='$photo1'");
			
		}
		else
		{
			$error .= $r1." - Invalid image format."; 	
		}	
	}
	if($r2 != "")
	{
		if($_FILES['r2']['type'] == "image/gif" || $_FILES['r2']['type'] == "image/pjpeg" || $_FILES['r2']['type'] == "image/jpeg")
		{
			$newupload = $_FILES['r2']['tmp_name'];
			list($width,$height)=getimagesize($newupload);
			
				$delpath1 = "homepageimg/".$q["RightImage2"];
				if(file_exists($delpath1)) unlink($delpath1);
				if($_FILES['r2']['type'] == "image/gif") $filetype = ".gif";
				else  $filetype = ".jpg";
				
				if($filetype==".gif") $src = imagecreatefromgif($newupload);
				else $src = imagecreatefromjpeg($newupload);
				list($width,$height)=getimagesize($newupload);
				
				if($width != 57 && $height !=85)
				{
					$newwidth = 57;
					$newheight = 85;
				}
				else
				{
					$newwidth = $width;
					$newheight = $height;
				}
				
				$tmp=imagecreatetruecolor($newwidth,$newheight);
				imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
				$photo1  = "r2".$filetype;
				$filename = $path.$photo1; 
				if($filetype==".gif") imagegif($tmp,$filename,100);
				else imagejpeg($tmp,$filename,100);
				imagedestroy($src);
				imagedestroy($tmp);
				
				mysql_query("update homecontent set RightImage2='$photo1'");
			
		}
		else
		{
			$error .= $r2." - Invalid image format."; 	
		}
	}
	if($error=="")
	{
		//header("location:index.php?_s3=true#rightimg");
		echo "<script>window.location='index.php?_s3=true#rightimg'</script>";
	}
	else
	{
		//header("location:index.php?_s3=false&e=$error#rightimg");	
		echo "<script>window.location='index.php?_s3=false&e=$error#rightimg'</script>";
	}
}

if($_GET['_mod']==3 && isset($_GET['_col']))
{
	$q = mysql_fetch_array(mysql_query("select * from homecontent limit 1"));
	if($_GET['_col']==1)
	{
		$path = "homepageimg/".$q["RightImage1"];
		@unlink($path);
		mysql_query("update homecontent set RightImage1=''");
	}
	if($_GET['_col']==2)
	{
		$path = "homepageimg/".$q["RightImage2"];
		@unlink($path);
		mysql_query("update homecontent set RightImage2=''");
	}
	//header("location:index.php?_s4=true#rightimg");
	echo "<script>window.location='index.php?_s4=true#rightimg'</script>";
}


if(isset($_POST['botbtn']) && $_REQUEST['_mod']==4)
{
	$b1 = $_FILES["b1"]["name"];
	$b2 = $_FILES["b2"]["name"];
	$b3 = $_FILES["b3"]["name"];
	$b4 = $_FILES["b4"]["name"];
	$b5 = $_FILES["b5"]["name"];
	$b6 = $_FILES["b6"]["name"];
	
	$error = "";
	$path = "homepageimg/";
	if($b1 != "")
	{
		$q = mysql_fetch_array(mysql_query("select * from homecontent limit 1"));
		if($_FILES['b1']['type'] == "image/gif" || $_FILES['b1']['type'] == "image/pjpeg" || $_FILES['b1']['type'] == "image/jpeg")
		{
			$newupload = $_FILES['b1']['tmp_name'];
			list($width,$height)=getimagesize($newupload);
			//if($width > 200 || $height > 110)
			{
				//$error .= $b1." - Invalid file size."; 	
			}
			//else
			{
				$delpath1 = "homepageimg/".$q["BottomImage1"];
				if(file_exists($delpath1)) unlink($delpath1);
				if($_FILES['b1']['type'] == "image/gif") $filetype = ".gif";
				else  $filetype = ".jpg";
				
				if($filetype==".gif") $src = imagecreatefromgif($newupload);
				else $src = imagecreatefromjpeg($newupload);
				list($width,$height)=getimagesize($newupload);
				
				/*if($width > 200 || $width < 200)
				{
					for($i=0;$i<100;$i++)
					{
						if($i == 0)
						{
							$newwidth=200;
							$w=$width/$newwidth;
							$newheight=$height/$w;
						}
						else
						{
							$newheight=$height/($w+($i*.02));
						}
						if($newheight==110) break;
					}
				}
				//echo $newheight; exit;
				if($newheight > 110 || $newheight < 110)
				{
					for($i=0;$i<100;$i++)
					{
						if($i == 0)
						{
							$newheight = 110;	
							$h = $height/$newheight;
							$newwidth = $width/$h;
						}
						else
						{
							$newwidth = $width/($h+($i*.02));
						}
						if($newwidth<=200) break;
					}
				}*/
				if($width != 200 && $height !=110)
				{
					$newwidth = 200;
					$newheight = 110;
				}
				else
				{
					$newwidth = $width;
					$newheight = $height;
				}
				//$newwidth = $width;
				//$newheight = $height;
				$tmp=imagecreatetruecolor($newwidth,$newheight);
				imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
				$photo1  = "b1".$filetype;
				$filename = $path.$photo1; 
				if($filetype==".gif") imagegif($tmp,$filename,100);
				else imagejpeg($tmp,$filename,100);
				imagedestroy($src);
				imagedestroy($tmp);
		
				//$img1 = "b1".$filetype;
				//$path1 = "homepageimg/".$img1;
				//move_uploaded_file(	$_FILES['b1']['tmp_name'],$path1);
				mysql_query("update homecontent set BottomImage1='$photo1'");
			}
		}
		else
		{
			$error .= $r1." - Invalid image format."; 	
		}	
	}
	if($b2 != "")
	{
		if($_FILES['b2']['type'] == "image/gif" || $_FILES['b2']['type'] == "image/pjpeg" || $_FILES['b2']['type'] == "image/jpeg")
		{
			$newupload = $_FILES['b2']['tmp_name'];
			list($width,$height)=getimagesize($newupload);
			
				$delpath1 = "homepageimg/".$q["BottomImage2"];
				if(file_exists($delpath1)) unlink($delpath1);
				if($_FILES['b2']['type'] == "image/gif") $filetype = ".gif";
				else  $filetype = ".jpg";
				
				if($filetype==".gif") $src = imagecreatefromgif($newupload);
				else $src = imagecreatefromjpeg($newupload);
				list($width,$height)=getimagesize($newupload);
				
				if($width != 200 && $height !=110)
				{
					$newwidth = 200;
					$newheight = 110;
				}
				else
				{
					$newwidth = $width;
					$newheight = $height;
				}
				
				$tmp=imagecreatetruecolor($newwidth,$newheight);
				imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
				$photo1  = "b2".$filetype;
				$filename = $path.$photo1; 
				if($filetype==".gif") imagegif($tmp,$filename,100);
				else imagejpeg($tmp,$filename,100);
				imagedestroy($src);
				imagedestroy($tmp);
				
				mysql_query("update homecontent set BottomImage2='$photo1'");
			
		}
		else
		{
			$error .= $b2." - Invalid image format."; 	
		}
	}
	if($b3 != "")
	{
		if($_FILES['b3']['type'] == "image/gif" || $_FILES['b3']['type'] == "image/pjpeg" || $_FILES['b3']['type'] == "image/jpeg")
		{
			$newupload = $_FILES['b3']['tmp_name'];
			list($width,$height)=getimagesize($newupload);
			
				$delpath1 = "homepageimg/".$q["BottomImage3"];
				if(file_exists($delpath1)) unlink($delpath1);
				if($_FILES['b3']['type'] == "image/gif") $filetype = ".gif";
				else  $filetype = ".jpg";
				
				if($filetype==".gif") $src = imagecreatefromgif($newupload);
				else $src = imagecreatefromjpeg($newupload);
				list($width,$height)=getimagesize($newupload);
				
				if($width != 200 && $height !=110)
				{
					$newwidth = 200;
					$newheight = 110;
				}
				else
				{
					$newwidth = $width;
					$newheight = $height;
				}
				
				$tmp=imagecreatetruecolor($newwidth,$newheight);
				imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
				$photo1  = "b3".$filetype;
				$filename = $path.$photo1; 
				if($filetype==".gif") imagegif($tmp,$filename,100);
				else imagejpeg($tmp,$filename,100);
				imagedestroy($src);
				imagedestroy($tmp);
				
				mysql_query("update homecontent set BottomImage3='$photo1'");
			
		}
		else
		{
			$error .= $b3." - Invalid image format."; 	
		}
	}
	
	if($b4 != "")
	{
		if($_FILES['b4']['type'] == "image/gif" || $_FILES['b4']['type'] == "image/pjpeg" || $_FILES['b4']['type'] == "image/jpeg")
		{
			$newupload = $_FILES['b4']['tmp_name'];
			list($width,$height)=getimagesize($newupload);
			
				$delpath1 = "homepageimg/".$q["BottomImage4"];
				if(file_exists($delpath1)) unlink($delpath1);
				if($_FILES['b4']['type'] == "image/gif") $filetype = ".gif";
				else  $filetype = ".jpg";
				
				if($filetype==".gif") $src = imagecreatefromgif($newupload);
				else $src = imagecreatefromjpeg($newupload);
				list($width,$height)=getimagesize($newupload);
				
				if($width != 200 && $height !=110)
				{
					$newwidth = 200;
					$newheight = 110;
				}
				else
				{
					$newwidth = $width;
					$newheight = $height;
				}
				
				$tmp=imagecreatetruecolor($newwidth,$newheight);
				imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
				$photo1  = "b4".$filetype;
				$filename = $path.$photo1; 
				if($filetype==".gif") imagegif($tmp,$filename,100);
				else imagejpeg($tmp,$filename,100);
				imagedestroy($src);
				imagedestroy($tmp);
				
				mysql_query("update homecontent set BottomImage4='$photo1'");
			
		}
		else
		{
			$error .= $b4." - Invalid image format."; 	
		}
	}
	if($b5 != "")
	{
		if($_FILES['b5']['type'] == "image/gif" || $_FILES['b5']['type'] == "image/pjpeg" || $_FILES['b5']['type'] == "image/jpeg")
		{
			$newupload = $_FILES['b5']['tmp_name'];
			list($width,$height)=getimagesize($newupload);
			
				$delpath1 = "homepageimg/".$q["BottomImage5"];
				if(file_exists($delpath1)) unlink($delpath1);
				if($_FILES['b5']['type'] == "image/gif") $filetype = ".gif";
				else  $filetype = ".jpg";
				
				if($filetype==".gif") $src = imagecreatefromgif($newupload);
				else $src = imagecreatefromjpeg($newupload);
				list($width,$height)=getimagesize($newupload);
				
				if($width != 200 && $height !=110)
				{
					$newwidth = 200;
					$newheight = 110;
				}
				else
				{
					$newwidth = $width;
					$newheight = $height;
				}
				
				$tmp=imagecreatetruecolor($newwidth,$newheight);
				imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
				$photo1  = "b5".$filetype;
				$filename = $path.$photo1; 
				if($filetype==".gif") imagegif($tmp,$filename,100);
				else imagejpeg($tmp,$filename,100);
				imagedestroy($src);
				imagedestroy($tmp);
				
				mysql_query("update homecontent set BottomImage5='$photo1'");
			
		}
		else
		{
			$error .= $b5." - Invalid image format."; 	
		}
	}
	if($b6 != "")
	{
		if($_FILES['b6']['type'] == "image/gif" || $_FILES['b6']['type'] == "image/pjpeg" || $_FILES['b6']['type'] == "image/jpeg")
		{
			$newupload = $_FILES['b6']['tmp_name'];
			list($width,$height)=getimagesize($newupload);
			
				$delpath1 = "homepageimg/".$q["BottomImage6"];
				if(file_exists($delpath1)) unlink($delpath1);
				if($_FILES['b6']['type'] == "image/gif") $filetype = ".gif";
				else  $filetype = ".jpg";
				
				if($filetype==".gif") $src = imagecreatefromgif($newupload);
				else $src = imagecreatefromjpeg($newupload);
				list($width,$height)=getimagesize($newupload);
				
				if($width != 200 && $height !=110)
				{
					$newwidth = 200;
					$newheight = 110;
				}
				else
				{
					$newwidth = $width;
					$newheight = $height;
				}
				
				$tmp=imagecreatetruecolor($newwidth,$newheight);
				imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
				$photo1  = "b6".$filetype;
				$filename = $path.$photo1; 
				if($filetype==".gif") imagegif($tmp,$filename,100);
				else imagejpeg($tmp,$filename,100);
				imagedestroy($src);
				imagedestroy($tmp);
				
				mysql_query("update homecontent set BottomImage6='$photo1'");
			
		}
		else
		{
			$error .= $b6." - Invalid image format."; 	
		}
	}
	
	
	if($error=="")
	{
		//header("location:index.php?_s6=true#botimg");
		echo "<script>window.location='index.php?_s6=true#botimg'</script>";
	}
	else
	{
		//header("location:index.php?_s6=false&e=$error#botimg");	
		echo "<script>window.location='index.php?_s6=false&e=$error#botimg'</script>";
	}
}

if($_GET['_mod']==5 && isset($_GET['_col']))
{
	$q = mysql_fetch_array(mysql_query("select * from homecontent limit 1"));
	if($_GET['_col']==1)
	{
		$path = "homepageimg/".$q["BottomImage1"];
		@unlink($path);
		mysql_query("update homecontent set BottomImage1=''");
	}
	if($_GET['_col']==2)
	{
		$path = "homepageimg/".$q["BottomImage2"];
		@unlink($path);
		mysql_query("update homecontent set BottomImage2=''");
	}
	if($_GET['_col']==3)
	{
		$path = "homepageimg/".$q["BottomImage3"];
		@unlink($path);
		mysql_query("update homecontent set BottomImage3=''");
	}
	if($_GET['_col']==4)
	{
		$path = "homepageimg/".$q["BottomImage4"];
		@unlink($path);
		mysql_query("update homecontent set BottomImage4=''");
	}
	if($_GET['_col']==5)
	{
		$path = "homepageimg/".$q["BottomImage5"];
		@unlink($path);
		mysql_query("update homecontent set BottomImage5=''");
	}
	if($_GET['_col']==6)
	{
		$path = "homepageimg/".$q["BottomImage6"];
		@unlink($path);
		mysql_query("update homecontent set BottomImage6=''");
	}
	//header("location:index.php?_s7=true#botimg");
	echo "<script>window.location='index.php?_s7=true#botimg'</script>";
}
?>